#include "MenuScene.h"
#include "GameScene.h"
#include "LoadingLayer.h"
#include "SettingsScene.h"
#include "HelpScene.h"
#include "ExitConfirmPopup.h"
#include "GlobalDefine.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;

CCScene* MenuScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    MenuScene *layer = MenuScene::create();
    
	scene->setTag(TAG_MENU);

    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MenuScene::init()
{
	if(g_isSound) 
	{
		CocosDenshion::SimpleAudioEngine *audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine();
		audioEngine->pauseBackgroundMusic();
	}

    // super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
    // add a "close" icon to exit the progress. it's an autorelease object
	CCMenuItemImage *pGameScene = CCMenuItemImage::create(
                                                          "bt_start.png",
                                                          "bt_start_select.png",
                                                          this,
                                                          menu_selector(MenuScene::goGameScene));

	CCMenuItemImage *pSettingsScene = CCMenuItemImage::create(
                                                          "bt_option.png",
                                                          "bt_option_select.png",
                                                          this,
                                                          menu_selector(MenuScene::goSettingsScene));

    CCMenuItemImage *pHelpScene = CCMenuItemImage::create(
                                                          "bt_help.png",
                                                          "bt_help_select.png",
                                                          this,
                                                          menu_selector(MenuScene::goHelpScene));

	CCMenuItemImage *pExitScene = CCMenuItemImage::create(
                                                          "bt_exit.png",
                                                          "bt_exit_select.png",
                                                          this,
                                                          menu_selector(MenuScene::exitGame));
    
    
	int startX = origin.x + pHelpScene->getContentSize().width/2 + 10;
	int startY = origin.y + pExitScene->getContentSize().height/2 + 10;

	pExitScene->setPosition(ccp(startX, startY));
	pHelpScene->setPosition(ccp(startX,  startY + pHelpScene->getContentSize().height * 1));
	pSettingsScene->setPosition(ccp(startX,  startY + pHelpScene->getContentSize().height * 2));
	pGameScene->setPosition(ccp(startX,  startY + pHelpScene->getContentSize().height * 3));

    // create menu, it's an autorelease object
    CCMenu* pHelp = CCMenu::create(pHelpScene, NULL);
    pHelp->setPosition(CCPointZero);
    this->addChild(pHelp, 1);
    
    CCMenu* pGame = CCMenu::create(pGameScene, NULL);
    pGame->setPosition(CCPointZero);
    this->addChild(pGame, 1);
    
    CCMenu* pSettings = CCMenu::create(pSettingsScene, NULL);
    pSettings->setPosition(CCPointZero);
    this->addChild(pSettings, 1);

	CCMenu* pExit = CCMenu::create(pExitScene, NULL);
    pExit->setPosition(CCPointZero);
    this->addChild(pExit, 1);
    
    // add your codes below...

    // add "MenuScene" splash screen"
    CCSprite* pSprite = CCSprite::create("menu.png");
    
    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    
    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    return true;
}

void MenuScene::goHelpScene(CCObject* pSender)
{
	if(g_isSound) 
	{
		CocosDenshion::SimpleAudioEngine *audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine();
		audioEngine->playBackgroundMusic("click.wav", false);
	}

	if(!ExitConfirmPopup::isShowing)
		CCDirector::sharedDirector()->replaceScene(HelpScene::scene());
}

void MenuScene::goSettingsScene(CCObject* pSender)
{
	if(g_isSound) 
	{
		CocosDenshion::SimpleAudioEngine *audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine();
		audioEngine->playBackgroundMusic("click.wav", false);
	}

	if(!ExitConfirmPopup::isShowing)
		CCDirector::sharedDirector()->replaceScene(SettingsScene::scene());
}

void MenuScene::goGameScene(CCObject* pSender)
{
	if(g_isSound) 
	{
		CocosDenshion::SimpleAudioEngine *audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine();
		audioEngine->playBackgroundMusic("click.wav", false);
	}

	if(!ExitConfirmPopup::isShowing)
		CCDirector::sharedDirector()->replaceScene(LoadingLayer::scene());
}

void MenuScene::exitGame(CCObject* pSender)
{
	if(g_isSound) 
	{
		CocosDenshion::SimpleAudioEngine *audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine();
		audioEngine->playBackgroundMusic("click.wav", false);
	}

	if(!ExitConfirmPopup::isShowing)
	{
		CCSize screenSize = CCDirector::sharedDirector()->getWinSize();

		ExitConfirmPopup* popup = ExitConfirmPopup::create();
		popup->setAnchorPoint(ccp(0.5f, 0.5f));
		popup->setPosition(ccp(screenSize.width * 0.5, screenSize.height * 0.5));
		this->addChild(popup, 2);
	}
}